package com.fh.modules.oms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.oms.entity.OmsOrderWechat;
import com.fh.modules.oms.service.IOmsOrderWechatService;
import com.fh.modules.oms.vo.OrderVo;
import com.fhMall.request.QueryOrderListRequest;
import com.fhMall.request.SubmitOrderRequest;
import com.fhMall.response.SubmitOrderResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * @Title: Controller
 * @Description: 微信订单表
 * @author： code generator
 * @date： 2019-10-07
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("微信订单表")
public class OmsOrderWechatController {

    @Autowired
    private IOmsOrderWechatService omsOrderWechatService;

    @GetMapping(value = "/oms/omsOrderWechat/list")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "微信订单表分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(QueryOrderListRequest queryOrderListRequest,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
//        OmsOrderWechat omsOrderWechat = new OmsOrderWechat();
//        BeanUtils.copyProperties(omsOrderWechat, queryOrderListRequest);
//        QueryWrapper<OmsOrderWechat> queryWrapper = new QueryWrapper<OmsOrderWechat>();
//        omsOrderWechat.setDelFlag(CommonConstant.DEL_FLAG_0);
//        QueryWrapperUtil.initQueryWrapper(queryWrapper, omsOrderWechat);
        LambdaQueryWrapper<OmsOrderWechat> query=new LambdaQueryWrapper();
        query.eq(OmsOrderWechat::getDelFlag,0);
        Page<OmsOrderWechat> page = new Page<OmsOrderWechat>(pageNo, pageSize);
        IPage<OmsOrderWechat> pageList = omsOrderWechatService.page(page, query);
        return Result.ok(pageList);
    }

    /**
     * 分页列表查询
     * @return
     */
    @PostMapping(value = "/oms/omsOrderWechat/wechatList")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "微信订单表分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(@RequestBody OrderVo orderVo) {
        return Result.okPage(omsOrderWechatService.queryOrder(orderVo));
    }

    @PostMapping(value = "/oms/omsOrderWechat/cancle")
    @ApiOperation("取消订单")
    @AutoLog("取消订单")
    public Result cancle(Integer id) {
        return Result.ok(omsOrderWechatService.cancelOrder(id));
    }

    @DeleteMapping(value = "/oms/omsOrderWechat/delete")
    @ApiOperation("删除订单")
    @AutoLog("删除订单")
    public Result delete(Integer id) {
        omsOrderWechatService.deleteEntityByDelFlag(id);
        return Result.ok("删除成功");
    }

    @PostMapping(value = "/oms/omsOrderWechat/payAgain")
    @ApiOperation("再次下单支付")
    @AutoLog("再次下单支付")
    public Result payAgain(@RequestParam(name = "orderId")Integer orderId,@RequestParam(name = "memberId")Integer memberId) {
        SubmitOrderResponse submitOrderResponse= omsOrderWechatService.payAgain(orderId,memberId);
        return Result.ok(submitOrderResponse);
    }

    @PostMapping(value = "/oms/omsOrderWechat/add")
    @ApiOperation("下订单")
    @AutoLog("微信下订单")
    public Result add(@RequestBody SubmitOrderRequest submitOrderRequest) {
        SubmitOrderResponse submitOrderResponse= omsOrderWechatService.order(submitOrderRequest);
        return Result.ok(submitOrderResponse);
    }

    @PutMapping(value = "/oms/omsOrderWechat/orderPay")
    @ApiOperation("订单支付")
    @AutoLog("订单支付")
    public Result orderPay(@RequestParam(name = "id") String id) {
        OmsOrderWechat omsOrderWechat = omsOrderWechatService.getById(id);
        omsOrderWechat.setWechatPrepayId(UUID.randomUUID().toString().replaceAll("-",""));
        omsOrderWechat.setModifyTime(new Date());
        omsOrderWechatService.save(omsOrderWechat);
        SubmitOrderResponse submitOrderResponse = SubmitOrderResponse.builder()
                .orderId(omsOrderWechat.getId())
                .wechatPrepayId(omsOrderWechat.getWechatPrepayId())
                .payPrice(omsOrderWechat.getPayPrice())
                .build();

        return Result.ok(submitOrderResponse);
    }

    @PutMapping(value = "/oms/omsOrderWechat/paySuccess")
    @ApiOperation("订单支付成功，业务处理")
    public void paySuccess(@RequestParam(name = "outTradeNo") String outTradeNo, @RequestParam(name = "transactionId") String transactionId){
        omsOrderWechatService.paySuccess(outTradeNo, transactionId);
    }

    @PostMapping(value = "/oms/omsOrderWechat/confirmRecieve")
    @ApiOperation("确认收货")
    @AutoLog("确认收货")
    public Result confirmRecieve(Integer orderId,Integer memberId) {
        omsOrderWechatService.confirmRecieve(orderId,memberId);
        return Result.ok("操作成功");
    }

    /**
     * 查看订单详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/oms/omsOrderWechat/detail")
    @ApiOperation("查看订单详情")
    @AutoLog(value = "查看订单详情", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result detail(@RequestParam(name = "id", required = true) String id) {
        OmsOrderWechat omsOrderWechat = omsOrderWechatService.getById(id);
        return Result.ok(omsOrderWechat);
    }

}
